﻿using psafth.RCTiming.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace psafth.RCTiming.Main
{
    public class InitOperation : BackgroundWorker
    {
        protected override void OnDoWork(DoWorkEventArgs e)
        {
            base.OnDoWork(e);

            int operations = 0;

            try
            {
                Club hmbk = new Club() { FullName = "Hanöbuktens Modellbåtklubb", Country = "Sweden", LicencePrefix = "S8-", ShortName = "HMBK" };
                Club erck = new Club() { FullName = "Eksjö RC-Klubb", Country = "Sweden", LicencePrefix = "S45-", ShortName = "ERCK" };
                Club smbc = new Club() { FullName = "Stockholm Model Boat Club", Country = "Sweden", LicencePrefix = "S6-", ShortName = "SMBC" };
                Club tmm = new Club() { FullName = "Team Middle-aged Men", Country = "Sweden", LicencePrefix = "S1-", ShortName = "TMM" };
                Club lmbk = new Club() { FullName = "Lerums Modellbåtklubb", Country = "Sweden", LicencePrefix = "S2-", ShortName = "LMBK" };

                Driver fredrikSvensson = new Driver() { FirstName = "Fredrik", LastName = "Svensson", LicenceNumber = "S8-49" };
                Driver johanEng = new Driver() { FirstName = "Johan", LastName = "Eng", LicenceNumber = "S8-6" };
                Driver perNilsson = new Driver() { FirstName = "Per", LastName = "Nilsson", LicenceNumber = "S8-95" };
                Driver marthBengtsson = new Driver() { FirstName = "Marth", LastName = "Bengtsson", LicenceNumber = "S8-2" };
                Driver danielHallstrom = new Driver() { FirstName = "Daniel", LastName = "Hållström", LicenceNumber = "S8-116" };
                Driver anderStenstrom = new Driver() { FirstName = "Anders", LastName = "Stenström", LicenceNumber = "S45-1" };
                Driver fredrickCederberg = new Driver() { FirstName = "Fredrick", LastName = "Cederberg", LicenceNumber = "S6-5" };
                Driver staffanZetterlund = new Driver() { FirstName = "Staffan", LastName = "Zetterlund", LicenceNumber = "S1-6" };
                Driver peterIngloff = new Driver() { FirstName = "Peter", LastName = "Ingloff", LicenceNumber = "S1-1" };
                Driver magnusHammarling = new Driver() { FirstName = "Magnus", LastName = "Hammarling", LicenceNumber = "S6-35" };
                Driver lennartJerksten = new Driver() { FirstName = "Lennart", LastName = "Jerksten", LicenceNumber = "S45-10" };
                Driver svenAndersson = new Driver() { FirstName = "Sven", LastName = "Andersson", LicenceNumber = "S2-46" };
                Driver stefanLarsson = new Driver() { FirstName = "Stefan", LastName = "Larsson", LicenceNumber = "S8-141" };

                Transponder tFS = new Transponder() { TransponderId = 4777687 };
                Transponder tJE = new Transponder() { TransponderId = 3581439 };
                Transponder tPN = new Transponder() { TransponderId = 9075509 };
                Transponder tMB = new Transponder() { TransponderId = 9530045 };
                Transponder tDH = new Transponder() { TransponderId = 8929082 };
                Transponder tAS = new Transponder() { TransponderId = 4917087 };
                Transponder tFC = new Transponder() { TransponderId = 6006846 };
                Transponder tSZ = new Transponder() { TransponderId = 9221969 };
                Transponder tPI = new Transponder() { TransponderId = 9796583 };
                Transponder tMH = new Transponder() { TransponderId = 9454387 };
                Transponder tLJ = new Transponder() { TransponderId = 5487897 };
                Transponder tSA = new Transponder() { TransponderId = 3056340 };
                Transponder tSL = new Transponder() { TransponderId = 6641950 };

                fredrikSvensson.Transponders.Add(tFS);
                johanEng.Transponders.Add(tJE);
                perNilsson.Transponders.Add(tPN);
                marthBengtsson.Transponders.Add(tMB);
                danielHallstrom.Transponders.Add(tDH);
                anderStenstrom.Transponders.Add(tAS);
                fredrickCederberg.Transponders.Add(tFC);
                staffanZetterlund.Transponders.Add(tSZ);
                peterIngloff.Transponders.Add(tPI);
                magnusHammarling.Transponders.Add(tMH);
                lennartJerksten.Transponders.Add(tLJ);
                svenAndersson.Transponders.Add(tSA);
                stefanLarsson.Transponders.Add(tSL);

                hmbk.Drivers.Add(fredrikSvensson);
                hmbk.Drivers.Add(johanEng);
                hmbk.Drivers.Add(perNilsson);
                hmbk.Drivers.Add(marthBengtsson);
                hmbk.Drivers.Add(danielHallstrom);
                hmbk.Drivers.Add(stefanLarsson);

                erck.Drivers.Add(anderStenstrom);
                erck.Drivers.Add(lennartJerksten);

                smbc.Drivers.Add(fredrickCederberg);
                smbc.Drivers.Add(magnusHammarling);

                tmm.Drivers.Add(peterIngloff);
                tmm.Drivers.Add(staffanZetterlund);

                lmbk.Drivers.Add(svenAndersson);

                RaceClass rc1 = new RaceClass() { Name = "FSR-M 1,8 standard" };
                RaceClass rc2 = new RaceClass() { Name = "FSR-M 1,8 special" };
                RaceClass rc3 = new RaceClass() { Name = "FSR-M 2,5 standard" };
                RaceClass rc4 = new RaceClass() { Name = "FSR-M 2,5 special" };
                RaceClass rc5 = new RaceClass() { Name = "FSR-V 3,5" };
                RaceClass rc6 = new RaceClass() { Name = "FSR-V 7,5" };
                RaceClass rc7 = new RaceClass() { Name = "FSR-V 15" };
                RaceClass rc8 = new RaceClass() { Name = "FSR-V 27" };
                RaceClass rc9 = new RaceClass() { Name = "FSR-V 35" };

                int threadSleepDuration = 20;

                // Count all items in db as operation. 
                operations += ((DBContext)e.Argument).Drivers.Count();
                operations += ((DBContext)e.Argument).Clubs.Count();
                operations += ((DBContext)e.Argument).Events.Count();
                operations += ((DBContext)e.Argument).Heats.Count();
                operations += ((DBContext)e.Argument).RaceClasses.Count();

                // Add the count for the soundfiles 
                operations += 12;

                double percentageValue = 100.0 / operations;

                // Let the mw know the percentValue
                ReportProgress(0, percentageValue);

                // Load the voices
                SoundPlayer.LoadSound("Voice/Saul_OneFinished.wav");
                ReportProgress(0, "Loading voice Saul_OneFinished.wav");
                Thread.Sleep(threadSleepDuration);

                SoundPlayer.LoadSound("Voice/Saul_TwoFinished.wav");
                ReportProgress(0, "Loading voice Saul_TwoFinished.wav");
                Thread.Sleep(threadSleepDuration);

                SoundPlayer.LoadSound("Voice/Saul_ThreeFinished.wav");
                ReportProgress(0, "Loading voice Saul_ThreeFinished.wav");
                Thread.Sleep(threadSleepDuration);

                SoundPlayer.LoadSound("Voice/Saul_FourFinished.wav");
                ReportProgress(0, "Loading voice Saul_FourFinished.wav");
                Thread.Sleep(threadSleepDuration);

                SoundPlayer.LoadSound("Voice/Saul_FiveFinished.wav");
                ReportProgress(0, "Loading voice Saul_FiveFinished.wav");
                Thread.Sleep(threadSleepDuration);

                SoundPlayer.LoadSound("Voice/Saul_SixFinished.wav");
                ReportProgress(0, "Loading voice Saul_SixFinished.wav");
                Thread.Sleep(threadSleepDuration);

                SoundPlayer.LoadSound("Voice/Saul_SevenFinished.wav");
                ReportProgress(0, "Loading voice Saul_SevenFinished.wav");
                Thread.Sleep(threadSleepDuration);

                SoundPlayer.LoadSound("Voice/Saul_EightFinished.wav");
                ReportProgress(0, "Loading voice Saul_EightFinished.wav");
                Thread.Sleep(threadSleepDuration);

                SoundPlayer.LoadSound("Voice/Saul_NineFinished.wav");
                ReportProgress(0, "Loading voice Saul_NineFinished.wav");
                Thread.Sleep(threadSleepDuration);

                SoundPlayer.LoadSound("Voice/Saul_TenFinished.wav");
                ReportProgress(0, "Loading voice Saul_TenFinished.wav");
                Thread.Sleep(threadSleepDuration);

                SoundPlayer.LoadSound("Voice/Saul_ElevenFinished.wav");
                ReportProgress(0, "Loading voice Saul_ElevenFinished.wav");
                Thread.Sleep(threadSleepDuration);

                SoundPlayer.LoadSound("Voice/Saul_TwelveFinished.wav");
                ReportProgress(0, "Loading voice Saul_TwelveFinished.wav");
                Thread.Sleep(threadSleepDuration);

                foreach (Club c in ((DBContext)e.Argument).Clubs)
                {
                    ReportProgress(0, string.Format("Loading club {0} {1}", c.LicencePrefix, c.FullName));
                    Thread.Sleep(threadSleepDuration);
                }

                foreach (Driver d in ((DBContext)e.Argument).Drivers)
                {
                    ReportProgress(0, string.Format("Loading driver {0} {1}", d.LicenceNumber, d.FullName));
                    Thread.Sleep(threadSleepDuration);
                }

                foreach (Event evt in ((DBContext)e.Argument).Events)
                {
                    ReportProgress(0, string.Format("Loading event {0}", evt.EventName));
                    Thread.Sleep(threadSleepDuration);
                }

                foreach (Heat h in ((DBContext)e.Argument).Heats)
                {
                    ReportProgress(0, string.Format("Loading heat {0}", h.Name));
                    Thread.Sleep(threadSleepDuration);
                }

                foreach (RaceClass r in ((DBContext)e.Argument).RaceClasses)
                {
                    ReportProgress(0, string.Format("Loading race class {0}", r.Name));
                    Thread.Sleep(threadSleepDuration);
                }

                using (DBContext db = new DBContext())
                {
                    // Fill DB with dummy data
                    if (db.Clubs.ToList().FirstOrDefault(c => c.ShortName.ToLower().Equals("hmbk")) == null)
                        db.Clubs.Add(hmbk);

                    if (db.Clubs.ToList().FirstOrDefault(c => c.ShortName.ToLower().Equals("lmbk")) == null)
                        db.Clubs.Add(lmbk);

                    if (db.Clubs.ToList().FirstOrDefault(c => c.ShortName.ToLower().Equals("erck")) == null)
                        db.Clubs.Add(erck);

                    if (db.Clubs.ToList().FirstOrDefault(c => c.ShortName.ToLower().Equals("smbc")) == null)
                        db.Clubs.Add(smbc);

                    if (db.Clubs.ToList().FirstOrDefault(c => c.ShortName.ToLower().Equals("tmm")) == null)
                        db.Clubs.Add(tmm);

                    if (db.RaceClasses.ToList().FirstOrDefault(r => r.Name.ToLower().Equals(rc1.Name.ToLower())) == null)
                        db.RaceClasses.Add(rc1);

                    if (db.RaceClasses.ToList().FirstOrDefault(r => r.Name.ToLower().Equals(rc2.Name.ToLower())) == null)
                        db.RaceClasses.Add(rc2);

                    if (db.RaceClasses.ToList().FirstOrDefault(r => r.Name.ToLower().Equals(rc3.Name.ToLower())) == null)
                        db.RaceClasses.Add(rc3);

                    if (db.RaceClasses.ToList().FirstOrDefault(r => r.Name.ToLower().Equals(rc4.Name.ToLower())) == null)
                        db.RaceClasses.Add(rc4);

                    if (db.RaceClasses.ToList().FirstOrDefault(r => r.Name.ToLower().Equals(rc5.Name.ToLower())) == null)
                        db.RaceClasses.Add(rc5);

                    if (db.RaceClasses.ToList().FirstOrDefault(r => r.Name.ToLower().Equals(rc6.Name.ToLower())) == null)
                        db.RaceClasses.Add(rc6);

                    if (db.RaceClasses.ToList().FirstOrDefault(r => r.Name.ToLower().Equals(rc7.Name.ToLower())) == null)
                        db.RaceClasses.Add(rc7);

                    if (db.RaceClasses.ToList().FirstOrDefault(r => r.Name.ToLower().Equals(rc8.Name.ToLower())) == null)
                        db.RaceClasses.Add(rc8);

                    if (db.RaceClasses.ToList().FirstOrDefault(r => r.Name.ToLower().Equals(rc9.Name.ToLower())) == null)
                        db.RaceClasses.Add(rc9);

                    db.SaveChanges();
                }

                //// Clear all events
                //foreach (Event ev in db.Events)
                //{
                //    ObservableCollectionExt<EventParticipant> participants = new ObservableCollectionExt<EventParticipant>(ev.Participants);
                //    foreach (EventParticipant ep in participants)
                //    {
                //        ev.Participants.Remove(ep);
                //    }
                //    db.Events.Remove(ev);
                //}

                ((DBContext)e.Argument).SaveChanges();

                ReportProgress(100, e.Argument);
            }
            // long-running operation goes here.
            catch (Exception ex)
            {

            }
        }
    }
}
